package com.lanou.hotel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lanou.hotel.entity.Type;
import com.lanou.hotel.service.ITypeService;
import com.lanou.hotel.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author msy
 * @since 2023-10-17
 */
@RestController
public class TypeController {

    @Autowired
    private ITypeService typeService;

    /**
     * 获取所有home类型
     * @return
     */
    @GetMapping("/type/getTypes")
    public ResultVO<List<Type>> getTypes(){
        List<Type> types = typeService.list(new LambdaQueryWrapper<Type>());
        if (types != null) {
            return ResultVO.success("请求成功",types);
        }else {
            return ResultVO.failed("请求失败");
        }
    }

    /**
     * 分页查询类型
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/type/getTypesForPage")
    public ResultVO<Map<String,Object>> getTypesForPage(int pageNum,int pageSize){
        return typeService.getTypes(pageNum,pageSize);
    }

    /**
     * 批量删除房类型
     * @param typeList
     * @return
     */
    @PostMapping("/type/deleteTypes")
    public ResultVO deleteTypes(@RequestBody List<Type> typeList){
        try{
            typeService.removeByIdsLinkHome(typeList);
            return ResultVO.success("删除成功",null);
        }catch (Exception e){
            e.printStackTrace();
            return ResultVO.failed("删除失败");
        }
    }

    /**
     * 创建类型
     * @param type
     * @return
     */
    @PostMapping("/type/createType")
    public ResultVO createType(@RequestBody Type type){
        boolean isSaved = typeService.save(type);
        if (isSaved) {
            return ResultVO.success("保存成功",null);
        }else {
            return ResultVO.failed("保存失败");
        }
    }

    /**
     * 保存已修改的房类型
     * @param type
     * @return
     */
    @PostMapping("/type/saveEditedType")
    public ResultVO saveEditedType(@RequestBody Type type){
        boolean isSave = typeService.updateById(type);
        if (isSave) {
            return ResultVO.success("修改成功",null);
        }else {
            return ResultVO.failed("修改失败");
        }
    }
}
